package com.ppx.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ppx.dao.InfoDao;
import com.ppx.pojo.Info;
import com.ppx.service.InfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author wangxiaotao
 * @create 2021-05-24-8:47
 */
@Service
public class InfoServiceImpl implements InfoService {
	@Autowired
	private InfoDao infoDao;

	//统计确诊人数
	@Override
	public int confirmCount() {
		return infoDao.confirmCount();
	}

	//查询出所有的省份
	@Override
	public List<String> queryAllProvinceName() {
		return infoDao.queryAllProvinceName();
	}

	//根据省份查询出该省份的疫情信息
	@Override
	public List<Map<String, Object>> queryEpidemicByProvinceName(String provinceName) {
		//查询数据库数据
		Info info = infoDao.queryEpidemicByProvinceName(provinceName);
		List<Map<String, Object>> hashMapList=new ArrayList<>();
		Map<String, Object>map1=new HashMap<>();
		//存值
		map1.put("name","现有确诊");
		map1.put("value",info.getConfirmCount());
		hashMapList.add(map1);
		Map<String, Object>map2=new HashMap<>();
		//存值
		map2.put("name","现有治愈");
		map2.put("value",info.getCuredCount());
		hashMapList.add(map2);
		Map<String, Object>map3=new HashMap<>();
		//存值
		map3.put("name","现有死亡");
		map3.put("value",info.getDeadCount());
		hashMapList.add(map3);
		return hashMapList;
	}

	//根据省份查询出该省份的人数前五的地区
	@Override
	public List<String[]> queryTopFiveConfrimByName(String provinceName) {
		List<HashMap<String, Object>> datalist = infoDao.queryTopFiveConfrimByName(provinceName);
		List<String[]> list=new ArrayList<>();
		if("香港".equals(provinceName)||"澳门".equals(provinceName)||"台湾".equals(provinceName)){
			list.add(new String[]{provinceName});
			list.add(new String[]{datalist.get(0).get("time").toString()});
			list.add(new String[]{datalist.get(0).get("confirmCount").toString()});
			return list;
		}
		String[] areaNames=new String[datalist.size()];
		String[] confirmCounts=new String[datalist.size()];
		for(int i=0;i<datalist.size();i++){
			areaNames[i]= datalist.get(i).get("areaName").toString();
			confirmCounts[i] = datalist.get(i).get("confirmCount").toString();
		}
		list.add(areaNames);
		list.add(new String[]{datalist.get(0).get("time").toString()});
		list.add(confirmCounts);
		return list;
	}

	//根据省份查询出该省份的治愈人数前五的地区
	@Override
	public List<String[]> queryCuredCountByName(String provinceName) {
		List<HashMap<String, Object>> datalist = infoDao.queryCuredCountByName(provinceName);
		List<String[]> list=new ArrayList<>();
		if("香港".equals(provinceName)||"澳门".equals(provinceName)||"台湾".equals(provinceName)){
			list.add(new String[]{provinceName});
			list.add(new String[]{datalist.get(0).get("curedCount").toString()});
			return list;
		}
		String[] areaNames=new String[datalist.size()];
		String[] confirmCounts=new String[datalist.size()];
		for(int i=0;i<datalist.size();i++){
			areaNames[i]= datalist.get(i).get("areaName").toString();
			confirmCounts[i] = datalist.get(i).get("curedCount").toString();
		}
		list.add(areaNames);
		list.add(confirmCounts);
		return list;
	}

	//根据省份查询出该省份的死亡人数前五的地区
	@Override
	public List<Map<String, Object>> queryDeadCountByName(String provinceName) {
		//查询数据库数据
		List<HashMap<String, Object>> hashMapListdata = infoDao.queryDeadCountByName(provinceName);
		List<Map<String, Object>> hashMapList=new ArrayList<>();
		if(hashMapListdata.size()==0){
			Map<String, Object>map=new HashMap<>();
			map.put("name","暂无数据");
			map.put("value",0);
			hashMapList.add(map);
			return hashMapList;
		}
		if("香港".equals(provinceName)||"澳门".equals(provinceName)||"台湾".equals(provinceName)){
			Map<String, Object>map=new HashMap<>();
			map.put("name",provinceName);
			map.put("value",hashMapListdata.get(0).get("deadCount"));
			hashMapList.add(map);
			return hashMapList;
		}
		for(int i=0;i<hashMapListdata.size();i++){
			Map<String, Object>map=new HashMap<>();
			map.put("name",hashMapListdata.get(i).get("areaName"));
			map.put("value",hashMapListdata.get(i).get("deadCount"));
			hashMapList.add(map);
		}
		return hashMapList;
	}

	//查询所有人数
	@Override
	public List<Map<String, Object>> queryAllCount() {
		//查询数据库数据
		Info info = infoDao.queryAllCount();
		List<Map<String, Object>> hashMapList=new ArrayList<>();
		Map<String, Object>map1=new HashMap<>();
		//存值
		map1.put("name","现有确诊");
		map1.put("value",info.getConfirmCount());
		hashMapList.add(map1);
		Map<String, Object>map2=new HashMap<>();
		//存值
		map2.put("name","现有治愈");
		map2.put("value",info.getCuredCount());
		hashMapList.add(map2);
		Map<String, Object>map3=new HashMap<>();
		//存值
		map3.put("name","现有死亡");
		map3.put("value",info.getDeadCount());
		hashMapList.add(map3);
		return hashMapList;
	}

	//按省份查询所有感染人数
	@Override
	public List<Map<String, Object>> queryConfirmCount() {
		//查询数据库数据
		List<Info> infoList= infoDao.queryConfirmCount();
		List<Map<String, Object>> hashMapList=new ArrayList<>();
		for (int i = 0; i < infoList.size(); i++) {
			Map<String, Object> map=new HashMap<>();
			map.put("name",infoList.get(i).getProvinceName());
			map.put("value",infoList.get(i).getConfirmCount());
			hashMapList.add(map);
		}
		return hashMapList;
	}

	//查询所有info信息
	@Override
	public HashMap<String, Object> queryAllInfo(int page, int limit) {
		HashMap<String,Object> resultdata = new HashMap<>(); //装前台当前页的数据
		Page pages= PageHelper.startPage(page,limit);
		List<Info> infoList=infoDao.queryAllInfo();
		if(infoList!=null){
			resultdata.put("code",0);
			resultdata.put("msg","疫情数据");
			resultdata.put("count", pages.getTotal());
			resultdata.put("data",infoList);
			return resultdata;
		}
		resultdata.put("code",200);
		return  resultdata;
	}

	//修改info数据
	@Override
	public String updataInfoById(String dataType, Integer data,Integer id) {
		if(infoDao.updataInfoById(dataType,data,id)==1)
			return "success";
		return "error";
	}

	//根据省份查询该省份的所有info数据
	@Override
	public HashMap<String, Object> queryAllInfoByProvinceName(int page, int limit, String provinceName) {
		HashMap<String,Object> resultdata = new HashMap<>(); //装前台当前页的数据
		Page pages= PageHelper.startPage(page,limit);
		List<Info> infoList=infoDao.queryAllInfoByProvinceName(provinceName);
		if(infoList!=null){
			resultdata.put("code",0);
			resultdata.put("msg",provinceName+"疫情数据");
			resultdata.put("count", pages.getTotal());
			resultdata.put("data",infoList);
			return resultdata;
		}
		resultdata.put("code",200);
		return  resultdata;
	}

	//点击地图查看的某省数据
	@Override
	public List<Map<String, Object>> queryEpidemicAreaMapByCity(String provinceName) {
		List<Info> infoList= infoDao.queryEpidemicAreaMapByCity(provinceName);
		List<Map<String, Object>> hashMapList=new ArrayList<>();
		for (int i = 0; i < infoList.size(); i++) {
			Map<String, Object> map=new HashMap<>();
			map.put("name",infoList.get(i).getAreaName());
			map.put("value",infoList.get(i).getConfirmCount());
			hashMapList.add(map);
		}
		return hashMapList;
	}

}
